<?php

namespace App\Http\Controllers;

use App\Common\Sql;
use App\Exceptions\ApiException;
use Illuminate\Support\Facades\DB;

class ServerList extends Base
{

    public function logic()
    {

        $object =  DB::select("select * from t_server_info as a where 
        online_num = (select min(online_num) from t_server_info where a.server_flag=server_flag and status = 1) 
        group by server_flag;");

        foreach ($object as $key => $value) {
            $object[$key]->server_id = $value->id;
            $object[$key]->player_num = $value->online_num;
            unset($object[$key]->id);
            unset($object[$key]->online_num);
        }

        return $this->returnData(
            [
                'server_list' => $object
            ]
        );
    }

    public function detail()
    {
        $db = DB::table('t_server_info')->where('status',1);
        if (!empty($this->param['id'])) {
            $db->where('id',$this->param['id']);
            $object = $db->first();
        } elseif (!empty($this->param['game_flag'])) {
            $db->where('server_flag',$this->param['game_flag']);
            $db->where('online_num','<',500);
            $db->orderBy('online_num','desc');
            $object = $db->first();
            if (empty($object)) {
                $object = DB::table('t_server_info')->where('server_flag',$this->param['game_flag'])->where('status',1)->where('online_num','>',500)->first();
            }
        } else {
            throw new ApiException('参数有误');
        }

        $object->server_id  = $object->id;
        $object->player_num = $object->online_num;
        unset($object->id);
        unset($object->online_num);
        return $this->returnData(['server_info' => $object]);
    }

}
